#ifndef __PROCESS_POOL_HPP__
#define __PROCESS_POOL_HPP__
#include <iostream>
#include <cstdlib>
#include <vector>
#include <unistd.h>
#include <sys/wait.h>
#include "Task.hpp"

// 管道类
class Channel
{
public:
    Channel(int fd, pid_t id):_wfd(_wfd),_subid(id)
    {
        _name = "Channel-" + std::to_string(_wfd) + "-" + std::to_string(_subid);
    }
    ~Channel()
    {}

    void Send(int code)
    {
        int n = write(_wfd, &code, sizeof(code));
        (void)n;
    }

    void Close()
    {
        close(_wfd);
    }

    void Wait()
    {
        pid_t rid = waitpid(_subid, nullptr, 0);
        (void)rid;
    }
    int getwfd()
    {
        return _wfd;
    }
    pid_t getsubid()
    {
        return _subid;
    }
    std::string getname()
    {
        return _name;
    }

private:
    int _wfd;
    pid_t _subid;
    std::string _name;
};

// 管道管理
class ChannelManager
{
public:
    ChannelManager():_next(0)
    {}
    ~ChannelManager()
    {}

    Channel& Select()
    {
        auto &c = _channels[_next++];
        _next %= _channels.size();
        return c;
    }


    void channelInser(int wfd, pid_t subid)
    {
        _channels.emplace_back(wfd, subid);
    }

    void StopSubProcess()
    {
        for(auto& c : _channels)
        {
            std::cout << "关闭:" << c.getname() << std::endl;
            c.Close();
        }
    }

    void WaitSubProcess()
    {
        for(auto& c : _channels)
        {
            std::cout << "回收:" << c.getname() << std::endl;
            c.Wait();
        }
    }

    void PrintChannel()
    {
        for(auto &c : _channels)
        {
            std::cout << c.getname() << std::endl;
        }
    }

private:
    std::vector<Channel> _channels;
    int _next;
};

// 进程池类
class ProcessPool
{
public:
    ProcessPool(int num):_process_num(num)
    {
        _tm.Register(PrintLog);
        _tm.Register(Download);
        _tm.Register(Upload);
    }

    void Work(int rfd)
    {
        while(true)
        {
            int code = 0;
            ssize_t n = read(rfd, &code, sizeof(code));
            if(n > 0)
            {
                if(n != sizeof(code))
                {
                    continue;
                }
                std::cout << "子进程[" << getpid() << "]读取到一个任务码：" << code << std::endl;
                _tm.Execute(code);
            }
            else if(n == 0)
            {
                std::cout << "子进程退出" << std::endl;
                break;
            }
            else
            {
                std::cout << "子进程读取错误" << std::endl;
                break;
            }
        }
    }

    bool Start()
    {
        for(int i = 0; i < _process_num; i++)
        {
            int pfd[2] = {0};
            int n = pipe(pfd);
            if(n < 0)
                return false;
            pid_t subid = fork();
            if(subid < 0)
            {
                return false;
            }
            else if(subid == 0)
            {
                // 子进程
                // pfd[0]表示读，pfd[1]表示写
                close(pfd[1]);
                Work(pfd[0]);
                close(pfd[0]);
                exit(0);
            }
            else
            {
                // 父进程
                _cm.channelInser(pfd[1], subid);
                close(pfd[0]);
            }
        }
        return true;
    }
    void Run()
    {
        int taskcode = _tm.Code();

        // 选择一个进程来执行任务
        auto& c = _cm.Select();
        std::cout << "选择了一个子进程: " << c.getname() << std::endl;
        // 发送一个任务
        c.Send(taskcode);
        std::cout << "发送了一个任务码: " << taskcode << std::endl;
    }

    void Stop()
    {
        _cm.StopSubProcess();
        _cm.WaitSubProcess();
    }
private:
    ChannelManager _cm;
    int _process_num;
    TaskManager _tm;
};

#endif










